package org.zecontech.plantvszombie.util;

import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.AreaEffectCloud;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.zecontech.plantvszombie.PlantVsZombieMod;

import java.util.Collection;

public class Helpers {

    public static void triggerOnDeathMobEffects(ServerLevel serverLevel, Entity.RemovalReason removalReason, LivingEntity livingEntity) {
        for (MobEffectInstance mobEffectInstance : livingEntity.getActiveEffects()) {
            mobEffectInstance.onMobRemoved(serverLevel, livingEntity, removalReason);
        }

        livingEntity.getActiveEffects().clear();
    }

    public static void spawnLingeringCloud(Level level, BlockPos pos, LivingEntity livingEntity) {
        Collection<MobEffectInstance> collection = livingEntity.getActiveEffects();
        if (!collection.isEmpty()) {
            AreaEffectCloud areaEffectCloud = new AreaEffectCloud(level, pos.getX(), pos.getY(), pos.getZ());
            areaEffectCloud.setRadius(2.5F);
            areaEffectCloud.setRadiusOnUse(-0.5F);
            areaEffectCloud.setWaitTime(10);
            areaEffectCloud.setDuration(300);
            areaEffectCloud.setPotionDurationScale(0.25F);
            areaEffectCloud.setRadiusPerTick(-areaEffectCloud.getRadius() / (float)areaEffectCloud.getDuration());

            for (MobEffectInstance mobEffectInstance : collection) {
                areaEffectCloud.addEffect(new MobEffectInstance(mobEffectInstance));
            }

            level.addFreshEntity(areaEffectCloud);
        }

    }

    /**
     * 判断是否消耗物品
     * @param player 玩家
     * @return 返回判断
     */
    public static boolean shouldConsumeItem(Player player) {
        return !player.getAbilities().instabuild && !player.isSpectator();
    }

    /**
     * 创建物品实体
     * @param level 世界
     * @param pos 位置
     * @param stack 物品堆栈
     * @return 新的物品实体
     */
    public static ItemEntity createItemEntity(Level level, BlockPos pos, ItemStack stack) {
        return new ItemEntity(level, pos.getX(), pos.getY(), pos.getZ(), stack);
    }

    /**
     * 获取实体名字
     * @param entityType 实体标签
     * @return 实体名字
     */
    public static String getEntityName(EntityType<?> entityType) {
        return BuiltInRegistries.ENTITY_TYPE.getKey(entityType).getPath();
    }

    /**
     * 获取物品名字
     * @param item 物品实例
     * @return 物品名字
     */
    public static String getItemName(Item item) {
        return BuiltInRegistries.ITEM.getKey(item).getPath();
    }

    /**
     * 包装资源路径
     * @param name 名称
     * @return 返回模组资源路径下的某一内容
     */
    public static ResourceLocation identifier(String name) {
        return ResourceLocation.fromNamespaceAndPath(PlantVsZombieMod.MOD_ID, name);
    }
}
